package com.songlanyun.modules.faq.controller.api;

import cn.hutool.core.map.MapUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.songlanyun.common.utils.PageUtils;
import com.songlanyun.common.utils.R;
import com.songlanyun.modules.faq.entity.SysFaqEntity;
import com.songlanyun.modules.faq.service.SysFaqService;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;
import java.util.Map;

//import org.apache.shiro.authz.annotation.RequiresPermissions;


/**
 * 常见问题
 *
 * @author wuchao
 * @email sunlightcs@gmail.com
 * @date 2020-12-30 16:30:06
 */
@RestController
@RequestMapping("api/sys/faq")
public class SysFaqApiController {
    @Autowired
    private SysFaqService sysFaqService;

    /**
     * 列表
     */
    @GetMapping("/list")
    //@RequiresPermissions("faq:sysfaq:list")
    public R list(@RequestParam Map<String, Object> params) {
        String keyword = MapUtil.getStr(params, "keyword");
        List<SysFaqEntity> list = sysFaqService.list(
                new LambdaQueryWrapper<SysFaqEntity>()
                        .and(o->{
                           o.like(StringUtils.isNotBlank(keyword),SysFaqEntity::getTitle,keyword)
                                   .or()
                                   .like(StringUtils.isNotBlank(keyword),SysFaqEntity::getContent,keyword);
                        })
                        .like(StringUtils.isNotBlank(keyword),SysFaqEntity::getTitle,keyword)
                        .orderByAsc(SysFaqEntity::getSort)
                        .orderByDesc(SysFaqEntity::getCreateTime)
        );

        return R.ok().put("list", list);
    }


    /**
     * 列表
     */
    @GetMapping("/pages")
    //@RequiresPermissions("faq:sysfaq:list")
    public R page(@RequestParam Map<String, Object> params) {
        PageUtils page = sysFaqService.queryPage(params);

        return R.ok().put("page", page);
    }



    /**
     * 信息
     */
    @GetMapping("/info/{id}")
    //@RequiresPermissions("faq:sysfaq:info")
    public R info(@PathVariable("id") Integer id) {
        SysFaqEntity sysFaq = sysFaqService.getById(id);

        return R.ok().put("data", sysFaq);
    }

}
